DATASET ACTIVATE DataSet1. * DHS Calendar Tutorial - Example 7. * Create events file. * download the model dataset for individual women's recode: "ZZIR62FL.SAV" * the model datasets are available at http://dhsprogram.com/data/download-model-datasets.cfm . * change to a working directory where the data are stored * or add the full path to the 'get file' command below. cd "C:\Data\DHS_model". * set up which calendar columns to look at - column numbers can vary across phases of DHS. * method use and pregnancies - always column 1. define !cal1() VCAL$1 !enddefine. * reasons for discontinuation - usually column 2. define !cal2() VCAL$2 !enddefine. * marriage - when it exists it is usually column 3 - adapt if it is a different column. define !cal3() VCAL$3 !enddefine. *define !cal3() !enddefine. * leave it blank if vcal$3 does not exist. * set length of calendar in a macro. define !vcal_len() 80 !enddefine. * open the dataset to use. get file="ZZIR62FL.SAV" / keep CASEID V000 V001 V002 V003 V005 V007 V008 V011 V017 V018 V019 V021 V023 V101 V102 V106 V190 !cal1 !cal2 !cal3. * set maximum number of loops high enough. * could be as many as the length of the calendar (typically 80), so set it a bit higher. set mxloops = 100. * Step 7.1. * set episode number - initialized to 0. compute eps = 0. * set previous calendar column 1 variable to anything that won't be in the calendar. string prev_vcal1 (a1). compute prev_vcal1 = "_". * create separate variables for each month of the calendar. vector vcal1_(!vcal_len A1). vector vcal2_(!vcal_len A1). vector vcal3_(!vcal_len A1). vector EV004_(!vcal_len F2.0). loop #i = 1 to !vcal_len. compute #j = !vcal_len-#i+1. * contraceptive method, non-use, or birth, pregnancy, or termination. compute vcal1_(#i) = char.substr(!cal1,#j,1). * reason for discontinuation. compute vcal2_(#i) = char.substr(!cal2,#j,1). * marriage. initialize to blank in case it does not exist. compute vcal3_(#i) = " ". * comment out the line below if the marriage column does not exist. compute vcal3_(#i) = char.substr(!cal3,#j,1). * set up parameter to add into reshape below, and collapse further below. * increase the episode number if there is a change in vcal$1. if (vcal1_(#i) <> prev_vcal1) eps = eps+1. * set the episode number. compute EV004_(#i) = eps. * save the vcal1 value for the next time through the loop. compute prev_vcal1 = vcal1_(#i). end loop. execute. * Step 7.2. * reshape the new month by month variables into a long format. varstocases /make vcal1 from vcal1_1 to vcal1_!vcal_len /make vcal2 from vcal2_1 to vcal2_!vcal_len /make vcal3 from vcal3_1 to vcal3_!vcal_len /make EV004 from EV004_1 to EV004_!vcal_len /index=i(!vcal_len) /drop=!cal1 !cal2 !cal3 eps prev_vcal1 /null=keep. Variables to Cases Notes |-----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |Output Created |31-AUG-2017 01:26:31 | |-----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |Comments | | |--------------|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |Input |Filter | | | |--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | |Weight | | | |--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | |Split File | | |-----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |Syntax |varstocases /make vcal1 from vcal1_1 to vcal1_80 /make vcal2 from vcal2_1 to vcal2_80 /make vcal3 from vcal3_1 to vcal3_80 /make EV004 from EV004_1 to EV004_80 /index=i(80 ) /drop=VCAL$1 VCAL$2 VCAL$3 eps prev_vcal1 /null=keep.| |--------------|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |Resources |Processor Time|00:00:00.02 | | |--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | |Elapsed Time |00:00:00.01 | |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| Generated Variables |-----|------| |Name |Label | |-----|------| |i || |-----|------| |vcal1|| |-----|------| |vcal2|| |-----|------| |vcal3|| |-----|------| |EV004|| |------------| Processing Statistics |-------------|---| |Variables In |343| |-------------|---| |Variables Out|23 | |-----------------| * label the event number variable. variable labels EV004 "Event number". * Step 7.3. * create the century month code (CMC) for each month. compute cmc=V017+i-1. print formats cmc (f2.0). * drop the blank episode after the date of interview. select if (i <= V019). * Step 7.4. * aggregate the episodes within each case, keeping start and end, the code, and other useful information. dataset declare Events. aggregate /outfile="Events" /break=CASEID EV004 /V001 =first(V001) /V002 =first(V002) /V003 =first(V003) /V005 =first(V005) /V007 =first(V007) /V008 =first(V008) /V011 =first(V011) /V017 =first(V017) /V018 =first(V018) /V019 =first(V019) /V021 =first(V021) /V023 =first(V023) /V101 =first(V101) /V102 =first(V102) /V106 =first(V106) /V190 =first(V190) /EV900 "CMC event begins"=first(cmc) /EV901 "CMC event ends"=last(cmc) /EV901A "Duration of event"=Nu(cmc) /EV902A "Event code (alpha)"=last(vcal1) /EV903A "Discontinuation code (alpha)"=last(vcal2) /EV906A "Married at end of episode (alpha)"=last(vcal3). dataset activate Events. * Step 7.5. * convert the event string variable for the episode (EV902a) to numeric (EV902). * set up a list of codes used in the calendar, * with the position in the string of codes being the code that will be assigned. * use a tilde (~) to mark gaps in the coding that are not used for this survey. * Emergency contraception (E), Other modern method (M) and Standard days method (S) * are recent additions as standard codes and may mean something different in earlier surveys. * note that some of the codes are survey specific so this will need adjusting. * do a frequency of vcal1_ to see the full list of codes to handle for the survey you are using. * convert the contraceptive methods to numeric codes, using the position in the string. compute EV902 = char.index("123456789WNALCFEMS~",EV902A). * now convert the birth, termination and pregnancy codes to 81, 82, 83 respectively. compute preg = char.index("BTP",EV902A). if (preg > 0) EV902 = preg+80. * convert the missing code to 99. if (EV902A = "?") EV902 = 99. * now check if there are any codes that were not converted, and change these to -1. if (EV902 = 0 & EV902A <> "0") EV902 = -1. execute. delete variables preg. * list cases where the event code was not recoded. compute filter_$ = (EV902 = -1). filter by filter_$. * there shouldn't be any cases listed. list variables = CASEID EV004 EV902 EV902A. List Notes |-----------------------------|-------------------------------------------| |Output Created |31-AUG-2017 01:26:37 | |-----------------------------|-------------------------------------------| |Comments | | |--------------|--------------|-------------------------------------------| |Input |Active Dataset|Events | | |--------------|-------------------------------------------| | |File Label |Aggregated File | | |--------------|-------------------------------------------| | |Filter |filter_$ | | |--------------|-------------------------------------------| | |Weight | | | |--------------|-------------------------------------------| | |Split File | | |-----------------------------|-------------------------------------------| |Syntax |list variables = CASEID EV004 EV902 EV902A.| |--------------|--------------|-------------------------------------------| |Resources |Processor Time|00:00:00.05 | | |--------------|-------------------------------------------| | |Elapsed Time |00:00:00.05 | |-------------------------------------------------------------------------| [Events] Number of cases read: 0 Number of cases listed: 0 filter off. * Step 7.6. * convert the discontinuation string variable for the episode (EV903a) to numeric (EV903). * set up a list of codes used in the calendar. * use a tilde (~) to mark gaps in the coding that are not used for this survey. * convert the reasons for discontinuation to numeric codes, using the position in the string. if (EV903A <> " ") EV903 = char.index("123456789CFAD~~~~",EV903A). * now convert the special codes for other, don't know and missing to 96, 98, 99 respectively. if (EV903A <> " ") special = char.index("W~K?",EV903A). if (special > 0) EV903 = special+95. * now check if there are any codes that were not converted, and change these to -1. if (EV903 = 0 & EV903A <> " ") EV903 = -1. execute. delete variables special. * list cases where the reason for discontinuation code was not recoded. compute filter_$ = (EV903 = -1). filter by filter_$. list variables=CASEID EV004 EV903 EV903A. List Notes |-----------------------------|-----------------------------------------| |Output Created |31-AUG-2017 01:26:37 | |-----------------------------|-----------------------------------------| |Comments | | |--------------|--------------|-----------------------------------------| |Input |Active Dataset|Events | | |--------------|-----------------------------------------| | |File Label |Aggregated File | | |--------------|-----------------------------------------| | |Filter |filter_$ | | |--------------|-----------------------------------------| | |Weight | | | |--------------|-----------------------------------------| | |Split File | | |-----------------------------|-----------------------------------------| |Syntax |list variables=CASEID EV004 EV903 EV903A.| |--------------|--------------|-----------------------------------------| |Resources |Processor Time|00:00:00.03 | | |--------------|-----------------------------------------| | |Elapsed Time |00:00:00.04 | |-----------------------------------------------------------------------| [Events] Number of cases read: 0 Number of cases listed: 0 filter off. delete variables filter_$. * Step 7.7. * capture the previous event by looking at the event for the previous episode for this woman. if (CASEID = lag(CASEID)) EV904 = lag(EV902). if (CASEID = lag(CASEID)) EV904X = lag(EV901a). * need to sort the data in reverse order of the episode to be able to use the lag function. sort cases by CASEID EV004(D). * capture the following event by looking at the event for the next episode for this woman. if (CASEID = lag(CASEID)) EV905 = lag(EV902). if (CASEID = lag(CASEID)) EV905X = lag(EV901a). * re sort back into order. sort cases by CASEID EV004(A). execute. * Step 7.8. * label the event file variables and values. variable labels EV902 "Event code". variable labels EV903 "Discontinuation code". variable labels EV904 "Prior event code". variable labels EV904X "Duration of prior event". variable labels EV905 "Next event code". variable labels EV905X "Duration of next event". print formats EV004 EV901A EV902 EV903 EV904 EV904X EV905 EV905X (F2.0). value labels EV902 EV904 EV905 0 "No method used" 1 "Pill" 2 "IUD" 3 "Injectable" 4 "Diaphragm" 5 "Condom" 6 "Female sterilization" 7 "Male sterilization" 8 "Periodic abstinence/Rhythm" 9 "Withdrawal" 10 "Other traditional methods" 11 "Norplant" 12 "Abstinence" 13 "Lactational amenorrhea method" 14 "Female condom" 15 "Foam and Jelly" 16 "Emergency contraception" 17 "Other modern method" 18 "Standard days method" 81 "Birth" 82 "Termination" 83 "Pregnancy" 99 "Missing" -1 "***Unknown code not recoded***". value labels EV903 0 "No discontinuation" 1 "Became pregnant while using" 2 "Wanted to become pregnant" 3 "Husband disapproved" 4 "Side effects" 5 "Health concerns" 6 "Access/availability" 7 "Wanted more effective method" 8 "Inconvenient to use" 9 "Infrequent sex/husband away" 10 "Cost" 11 "Fatalistic" 12 "Difficult to get pregnant/menopause" 13 "Marital dissolution" 96 "Other" 98 "Don't know" 99 "Missing" -1 "***Unknown code not recoded***". * Step 7.9. * convert marriage codes to numeric, if it exists. compute EV906 = 7. if (EV906A = "0") EV906=0. if (EV906A = "X") EV906=1. if (EV906A = "?") EV906=9. variable labels EV906 "Married at end of episode". value labels EV906 0 "Not married" 1 "Married" 7 "Unknown code" 9 "Missing". print formats EV906 (f1.0). * Step 7.10. * save the events file. save outfile="eventsfile.sav".